using Godot;
using Godot.Collections;
using System;

public partial class DialogManager : Node2D
{
	public Dictionary<string,Variant> dialog;
	private UiDialog _uiDialog;
	public Array<string> dialogtext;
	public Dictionary<string,Variant> dialogdic;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		var UIControl = GetNode<UIControl>("/root/UIControl");
		dialog = LoadDialog();
		_uiDialog = UIControl.GetGuiViewManager().GetChild(0).GetNode<UiDialog>("%UiDialog");
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public Dictionary<string,Variant> LoadDialog()
	{
		Dictionary<string,Variant> dialog = new Dictionary<string,Variant>();
		string path = "res://content.json";
		if(!FileAccess.FileExists(path))
		{return null;}
		var file = FileAccess.Open(path,FileAccess.ModeFlags.Read);
		var text = file.GetAsText();
		dialog = Json.ParseString(text).AsGodotDictionary<string,Variant>();
		return dialog;
	}

	public void DisplayDialog(string type,string name)
	{
		Dictionary<string,Variant> obj = dialog[type].AsGodotDictionary<string,Variant>();

		dialogdic = obj[name].AsGodotDictionary<string,Variant>();
		//dialogtext = text["text"].AsGodotArray<string>();
		var UIControl = GetNode<UIControl>("/root/UIControl");
		UIControl.GetGuiViewManager().open_view("UiDialog");
		//_uiDialog.GetNode<Label>("%content").Text = text["text"];
	}

	
}
